package se19team1e.gui.awt;

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import se19team1e.controller.Application;
import se19team1e.controller.CreateJobAdvertController;
import se19team1e.entity.CompanyDTO;
import se19team1e.entity.DepartmentDTO;
import se19team1e.entity.JobAdvertDTO;
import se19team1e.exception.DataAccessException;
import se19team1e.gui.CreateJobAdvertForm;
import se19team1e.gui.MessageBoxHelper;
import java.awt.FlowLayout;

/**
 * 
 * A GUI form that allows the advertiser to input and add new job adverts.
 * 
 * @author lai shiaw san
 * 
 */
@SuppressWarnings("serial")
public class AWTCreateJobAdvertForm extends AWTBaseScreen implements
		CreateJobAdvertForm {

	/**
	 * A reference to the current owning controller of this form.
	 */
	private CreateJobAdvertController controller;

	/**
	 * The job advert input panel.
	 */
	private final JobAdvertInputPanel jobAdvertInputPanel;

	/**
	 * An awt button to submit login credentials.
	 */
	private final Button submitButton;

	/**
	 * Constructs a create job advert form, initializing with the application
	 * instance.
	 * 
	 * @param application
	 *            Reference to the current application instance.
	 */
	@SuppressWarnings("deprecation")
	public AWTCreateJobAdvertForm(Application application) {
		super(application);

		// this.setBounds(150, 50, 575, 659 );
		// initialize layout
		getContentPane().setLayout(new BorderLayout());

		// create panel to hold all sub panels
		Panel mainPanel = new Panel();
		mainPanel.setLayout(new BorderLayout());

		// create panel to hold text field
		this.jobAdvertInputPanel = new JobAdvertInputPanel();

		// add panel to center panel
		mainPanel.add(this.jobAdvertInputPanel, BorderLayout.CENTER);

		// create panel to hold buttons
		Panel buttonPanel = new Panel();

		// add panel to center panel
		mainPanel.add(buttonPanel, BorderLayout.SOUTH);

		// create and add submit button
		this.submitButton = new Button("Submit");
		// add callback for submit button
		this.submitButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				try {
					createJobAdvert();
				} catch (DataAccessException e1) {
					e1.printStackTrace();
				}
			}
		});
		buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
		buttonPanel.add(this.submitButton);

		// add panel to frame
		getContentPane().add(mainPanel);

		// pack the layout
		this.pack();

		// hide the frame
		this.hide();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see se19team1e.IBaseScreen#updateAvailableActions()
	 */
	@Override
	public void updateAvailableActions() {
		this.updateAvailableMenuActions();

		// ensure submit button is enabled
		submitButton.setEnabled(true);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see se19team1e.gui.CreateJobAdvertForm#initialize(se19team1e.controller.
	 * CreateJobAdvertController, java.util.List, java.util.List)
	 */
	@Override
	public void initialize(CreateJobAdvertController controller,
			List<CompanyDTO> companies, List<DepartmentDTO> departments) {
		this.controller = controller;

		// initialize the form
		this.jobAdvertInputPanel.initialize(new JobAdvertDTO(), companies, departments);
	}

	/**
	 * Triggers a creation of the current entered job advert info.
	 * 
	 * @throws DataAccessException
	 */
	public void createJobAdvert() throws DataAccessException {
		// validate first
		if (!this.validateInput()) {
			// failure, return
			return;
		}

		// delegate to controller
		this.controller.createJobAdvert(this.jobAdvertInputPanel
				.newJobAdvertDTOFromInput());
	}

	/**
	 * Validates the job advert input to see if it is valid for creation.
	 * 
	 * <p>
	 * Delegates to the login input panel.
	 * </p>
	 * 
	 * @return True if valid.
	 */
	private boolean validateInput() {
		return this.jobAdvertInputPanel.validateInput(new MessageBoxHelper(this
				.getApplication()));
	}

}
